package com.atguigu02.filestream.exer1;

import org.junit.Test;

import java.io.*;

/**
 * ClassName: FilestreamExer1Test
 * Package: com.atguigu02.filestream.exer1
 * Description:
 *
 * @Author BuTang
 * @Create 2023/6/14 17:50
 * @Version 1.0
 */
public class FilestreamExer1Test {
    public static void main(String[] args) throws IOException {
        File file = new File("hello.txt");
        System.out.println(file.getAbsoluteFile());
        FileReader fr = new FileReader(file);

        int data;
        while ((data = fr.read()) != -1){
        System.out.print((char) data);
        //fr.read每次都会读取一个值，执行一次便会读取下一个值
    }
        fr.close();

    }

    /**
     * 使用字符流复制learning.jpg文件，创建并保存为learning_1.jpg
     */

    @Test
    public void test1() throws IOException {

        //创建复制的文件
        File file1 = new File("learning.jpg");
        File file2 = new File("learning_1.jpg");
        boolean newFile = file2.createNewFile();

        //创建字符流
        FileInputStream fileInputStream = new FileInputStream(file1);
        FileOutputStream fileOutputStream = new FileOutputStream(file2);
        int len ;
        byte[] buffer = new byte[5];
        while ((len = fileInputStream.read(buffer)) != -1){
            fileOutputStream.write(buffer,0,len);
        }

        fileInputStream.close();
        fileOutputStream.close();
    }


}
